import { request } from "@/service/request/index";
import { User } from "@/types/user";
import { UserCreateDto, UserSearchDto } from "./dto/user.dto";

export const queryUsers = async (
    page: number,
    size: number,
    condition?: Partial<UserSearchDto>
): Promise<{
    count: number;
    users: User[];
}> => {
    try {
        return await request("/user/search-user", {
            page: page,
            size: size,
            ...condition,
        });
    } catch (err) {
        throw err;
    }
};

export const createUser = async (user: Partial<User>) => {
    try {
        return await request("/user/register", user);
    } catch (err) {
        throw err;
    }
};

export const updateUser = async (data: Partial<User>) => {
    try {
        return await request("/user/edit", data);
    } catch (err) {
        throw err;
    }
};

export const updateUserName = async (account: string, username: string) => {
    try {
        return await request("/user/edit", {
            account: account,
            userName: username,
        });
    } catch (err) {
        throw err;
    }
};

export const resetPassword = async (account: string, password: string) => {
    try {
        return await request("/user/change-password", {
            character: account,
            password: password,
        });
    } catch (err) {
        throw err;
    }
};

export const deleteUser = async (id: string) => {
    try {
        return await request("/user/delete", { id: id });
    } catch (err) {
        throw err;
    }
};
